package com.zlk.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.core.env.Profiles;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
import java.util.ArrayList;

@Configuration //配置类
@EnableSwagger2// 开启Swagger2的自动配置
public class SwaggerConfig {
    @Bean
    public Docket docket(Environment environment) {

        // 设置要显示swagger的环境
        Profiles of = Profiles.of("dev", "pro","test");
        // 判断当前是否处于该环境
        boolean b = environment.acceptsProfiles(of);
        // 通过 enable() 接收此参数判断是否要显示

        //配置了Swagger的Docket容器的Bean实例
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .groupName("星悦糖")
                .enable(b)//配置是否启用Swagger，如果是false，在浏览器将无法访问
                //通过.select()方法，去配置扫描接口,RequestHandlerSelectors配置如何扫描接口
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.zlk.controller"))
                // 配置如何通过path过滤,即这里只扫描请求以/zlk开头的接口
                .paths(PathSelectors.ant("/zlk/**"))
                .build();
    }
    //配置Swagger
    private ApiInfo apiInfo(){
        //开发文档的联系人信息
        Contact contact = new Contact("zlk", "https://blog.csdn.net/friggly?spm=1000.2115.3001.5343", "zlk@163.com");
        //配置Swagger信息
        return new ApiInfo(
                "SpringBoot整合Swagger使用",
                "SpringBoot整合Swagger,详细信息...",
                "1.0",
                "urn:tos",
                contact,
                "Apache 2.0",
                "http://www.apache.org/licenses/LICENSE-2.0",
                 new ArrayList());
    }
}